package com.spark.concurrency.other;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CountDownLatch;

/**
 * 一个或者多个线程，等待其他多个线程完成某件事情之后才能执行
 */
public class CountDownLatchTest {
    //这里的数量，是要等待完成的子线程数量，需要注意，
    private static CountDownLatch countDownLatch = new CountDownLatch(100);

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < countDownLatch.getCount(); i++) {
            new Thread(() -> {
                String date = LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME);
                System.out.println(Thread.currentThread().getName() + " Say:我准备好了，时间是：" + date);
                //单个线程准备好后，调用自减去，
                countDownLatch.countDown();
            }
            ).start();
        }
        //等待所有线程都准备好，
        countDownLatch.await();
        //大家都准备好了，
        System.out.println("那我们出发吧，时间是：" + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
    }
}
